﻿using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Options;
using Silk.NET.Vulkan;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample66
{
    [ModulePriority(ModulePriority.ISEA)]
    public class MainPipeModule : VulkanModule, IVulkanRenderWapperModule
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lightManager"></param>
        public MainPipeModule()
        {
        }

        public VulkanRenderWapper RenderWapper { get; set; }

        protected override Task Load(VulkanGraphicsManager graphicsManager)
        {
            var lDevice = graphicsManager.VulkanDevice;
            var pipeCache = lDevice.PipelineCache;
            var disapatcher = RenderWapper.GetRenderDispatcher<VulkanSwapchainRenderDispatcher>();
            var pass = disapatcher.GetSwapchainRenderPass();
            var area = RenderWapper.RenderArea;
            var basePath = AppContext.BaseDirectory;
            var descriptorPath = Path.Combine(basePath, "Assets");

            var command = lDevice.CreateCommandPool((device, option) =>
            {
                option.Flags = CommandPoolCreateFlags.CommandPoolCreateResetCommandBufferBit;
                option.QueueFamilyIndex = device.WorkQueues[0].FamilyIndex;
            }).CreatePrimaryCommandBuffer();
            var fence = lDevice.CreateFence();
            var mainGroup = graphicsManager.CreatePrimaryRenderGroup("MainRenderGroup", null)
                .Binding(command, lDevice, pass, new Semaphore[] { disapatcher.ImageAvailableSemaphore, disapatcher.RenderFinishSemaphore }, new Fence[] { fence.RawHandle });
            mainGroup.CreateGroupRenderingAction((builder) => builder.UseMiddleware<MainGroupRenderingMiddleware>());

            //_LightManager.CreateParallelLight("main", new Vector3(0, 100, 100), new LightStrength(0.9f, 0.9f, 0.5f, 0.8f));
            //_LightManager.Ambient = new Ambient(0.1f, 0.1f, 0.1f, 0.1f);
            var camera = graphicsManager.CreateCamera("main", new Vector3(0, 0, 600.0f), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
            var aspect = (float)area.Width / area.Height;
            camera.SetProjectMatrix(-aspect, 1.0f, 4, 5000);

            RenderWapper.CreateAndCutScene<Scene>("main", mainGroup, typeof(EarthSystem), typeof(MoonSystem), typeof(StarSystem), typeof(LightSystem));
            return Task.CompletedTask;
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
